package org.xjtu.weteam.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.xjtu.weteam.common.JsonResponse;
import org.xjtu.weteam.model.domain.UserTask;
import org.xjtu.weteam.service.UserTaskService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Kevin
 */
@RestController
@RequestMapping("/api/userTask")
public class UserTaskController {

    private final Logger logger = LoggerFactory.getLogger(UserTaskController.class);

    @Autowired
    private UserTaskService userTaskService;

    @PostMapping("/saveTaskUserRelationship")
    @ResponseBody
    public JsonResponse saveTaskUserRelationship(@RequestBody ArrayList<UserTask> userTasks){
        return JsonResponse.success(userTaskService.saveBatchs(userTasks));
    }

    @GetMapping("/searchUserProfilesByTaskId")
    @ResponseBody
    public JsonResponse searchUserProfilesByTaskId(Integer taskId) throws IOException {
        return JsonResponse.success(userTaskService.searchUserProfilesByTaskId(taskId));
    }

    @PostMapping("/getTaskUsers")
    @ResponseBody
    public JsonResponse getTaskUsers(Integer taskId){
        return JsonResponse.success(userTaskService.getTaskUsers(taskId));
    }

    @PostMapping("/removeUserTask")
    @ResponseBody
    public void removeUserTask(Integer taskId, List<Integer> userIds){
        userTaskService.removeRelationship(taskId, userIds);
    }
}
